//获取应用实例
const app = getApp()

Page({
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    agree: true, //同意列表是否显示
    noExamine: true, //  是否显示未审核面板
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    StatusBar: app.globalData.StatusBar, //状态栏高度
    CustomBar: app.globalData.CustomBar, //头部nav高度
    backIcon: true, // 返回按钮是否显示
    title: "审核悬赏", //页面标题
    // 审核未审核对象
    navItem: [{
        name: "未审核",
        number: 0,
        active: "active"
      },
      {
        name: "已审核",
        number: 0,
        active: ""
      }
    ],
    //未审核列表对象
    list: [],
    agreeList: [],
    noData: false,
    nowIndex: 0,
    currentPage: 1,
    pageSize: 9,
    totalPage: 1,
    examinedCurrentPage: 1,
    examinedPageSize: 9,
    examinedTotalPage: 1,
  },
  // 跳转到审核表单页面
  toExamine(e) {
    var id = e.currentTarget.dataset.id
    // console.log(e)
    wx: wx.navigateTo({
      url: '../reward/examine/examine?id=' + id
    })
  },
  // 跳转到审核详情页面
  toDetail(e) {
    var id = e.currentTarget.dataset.id
    // console.log(e)
    wx: wx.navigateTo({
      url: '../reward/detail/detail?id=' + id
    })
  },
  //审核未审核切换事件
  navTap(e) {
    var index = e.target.dataset.index,
      item = 'navItem[' + index + '].active',
      item2,
      noExamine;
    if (index === 0) {
      item2 = 'navItem[' + 1 + '].active'
      noExamine = true;
      console.log('index',0)
    } else {
      item2 = 'navItem[' + 0 + '].active'
      noExamine = false;
      console.log('index',1)
      // this.getExamineList()
      if (!this.data.isLoad) {
        this.getList(false)
      }
    }
    this.setData({
      [item]: "active",
      [item2]: "",
      noExamine: noExamine,
      nowIndex: index
    })
  },
  getList() {
    const that = this
    wx.showLoading({
      title: '加载中...',
    })
    const data = {
      currentPage: that.data.currentPage,
      pageSize: that.data.pageSize,
      userCode: wx.getStorageSync('userCode'),
      audit: true
    }
    // console.log(none)
    wx.request({
      url: app.globalData.baseUrl + 'reward/getAuditReward.do',
      method: 'post',
      data: data,
      success(res) {
        console.log('未审核',res)
        if (res.data.rstype) {
          const historyList = that.data.list
          const requestList = res.data.rewardList
          var list = that.pushData(historyList, requestList)
          that.setData({
            list: list,
            isLoad: true,
            ['navItem[0].number']: res.data.page.totalResult,
            totalPage: res.data.page.totalPage
          })

          //如果数据为空则显示无数据图片，否则显示列表
          if (res.data.rewardList.length > 0) {
            that.setData({
              noData: false
            })
          } else {
            that.setData({
              noData: true
            })
          }

          wx.hideLoading()
          wx.stopPullDownRefresh()
        }
      }
    })
  },
  getExamineList() {
    const that = this
    wx.showLoading({
      title: '加载中...',
    })
    const data = {
      currentPage: that.data.currentPage,
      pageSize: that.data.pageSize,
      userCode: wx.getStorageSync('userCode'),
      audit: false
    }
    // console.log(none)
    wx.request({
      url: app.globalData.baseUrl + 'reward/getAuditReward.do',
      method: 'post',
      data: data,
      success(res) {
        console.log('getExamineList',res)
        if (res.data.rstype) {
          const historyList = that.data.agreeList
          const requestList = res.data.rewardList
          var list = that.pushData(historyList, requestList)

          that.setData({
            agreeList: list,
            isLoad: true,
            ['navItem[1].number']: res.data.page.totalResult,
            examinedTotalPage: res.data.page.totalPage
          })

          //如果数据为空则显示无数据图片，否则显示列表
          if (res.data.rewardList.length > 0) {
            that.setData({
              noData: false
            })
          } else {
            that.setData({
              noData: true
            })
          }

          wx.hideLoading()
          wx.stopPullDownRefresh()
        }
      }
    })
  },
  pushData(srcData, oprateData) {
    var tempArr = srcData
    oprateData.forEach(item => {
      tempArr.push(item)
    })
    return tempArr
  },
  onShow() {
    this.loadPage()
    
  },
  onPullDownRefresh() {
    this.loadPage()
  },
  loadPage() {
    if (this.data.nowIndex === 0) {
      this.setData({
        list: [],
        currentPage: 1,
        totalPage: 1,
      })
      this.getList()
      //获取已审核数据
      this.getExamineList()
    } else {
      this.setData({
        agreeList: [],
        examinedCurrentPage: 1,
        examinedTotalPage: 1,
      })
      this.getExamineList()
    }
  },
  onReachBottom() {
    const nowIndex = this.data.nowIndex
    if (nowIndex === 0) {
      const currentPage = this.data.currentPage
      const totalPage = this.data.totalPage
      if (currentPage < totalPage) {
        this.setData({
          currentPage: currentPage + 1
        })
        this.getList()
      }
    } else {
      const examinedCurrentPage = this.data.examinedCurrentPage
      const examinedTotalPage = this.data.examinedTotalPage
      if (examinedCurrentPage < examinedTotalPage) {
        this.setData({
          examinedCurrentPage: examinedCurrentPage + 1
        })
        this.getExamineList()
      }
    }
  }
})